home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / CAS_App.c next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  51.9 KB  |  2,234 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CAS_App.c
  3.  
  4.     Contains:    This is the standard application app.c file. It contains routines
  5.                 to implement many things that must be in each application.
  6.  
  7.     Written by:    David H Nelson
  8.  
  9.     Copyright © 1993-1995 ComponentWorks, All rights reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13.         <15>      5/19/95    SJF        Bracket all modal dialog calls with CARequestModalFocus.
  14.                                     App_GetFrontDocWindow uses CAGetFrontDocWindow now.
  15.         --------------------------------------
  16.                                     
  17.                  05/19/95    SJF        Quit crashes when a saved document
  18.                                      has been opened because a window
  19.                                     has been inserted in our window list
  20.                                     that we don't own...
  21.         --------------------------------------
  22.  
  23.                  05/12/95    RB        Added stuff for new CALib menu 
  24.                                      Fixed Edit menu selection to be cooperative
  25.                                      with the ProxyPart
  26.         --------------------------------------
  27.                  05/09/95    SJF        Tweaks for WWDC Demo, See previous revision 
  28.                                      if we've trampled on some required changes.
  29.         --------------------------------------
  30.  
  31.                  05/08/95    SJF        Don't quit when the last window gets closed
  32.         --------------------------------------
  33.  
  34.                  04/23/95    RB        Mods to App_AdjustMenus
  35.         --------------------------------------
  36.                  03/31/95    RB        Moved code from App_CloseMenu(), which selects the
  37.                                      next doc window, into Win_Close().
  38.         --------------------------------------
  39.                  03/29/95    RB        Added call to CARegisterFloatingWindow() for the
  40.                                      Tool palette.
  41.         --------------------------------------
  42.                  03/27/95    DAS        converted "theDoc->changed" reference into API call
  43.         --------------------------------------
  44.                  03/16/95    RB        CALIB: Changing App_OpenFile to filter for files
  45.                                      saved using the Bento storage model.
  46.         --------------------------------------
  47.  
  48.                  01/20/95    RB        Reworked appAdjustCursor to correctly calculate
  49.                                      mouseRgn
  50.         --------------------------------------
  51.                  01/18/95    DHN        global function and variable name changes to suggest
  52.                                      OOP design. Split app.c file into app.c, win.c, doc.c,
  53.                                      item.c, and util.c.
  54.         --------------------------------------
  55.                  01/17/95    DAS        changed all FrontWindow() calls to
  56.                                      App_GetFrontDocWindow() to account for
  57.                                      floating windows. Added Win_IsToolWindow().
  58.                   1/16/95    DHN        Massive improvements including, multiple window
  59.                                       support, new window offsetting and naming, pref
  60.                                     file support, file saving/reading, cursor tracking,
  61.                                     PowerPC support, window update/activate in dialog
  62.                                     filter, item drawing/selection/cut/copy (not paste),
  63.                                     drag & drop (no drop yet), window scrolling, get
  64.                                     info dialog, Select all menu item, Tools menu.
  65.                     through
  66.                  12/23/94    DHN        Created CASample from existing Light Software
  67.                                      application framework.
  68.         --------------------------------------
  69.                   1/24/93    DHN        Added code to ignore the enter key. Fixed page 
  70.                                       flipping animation by using current GrafPort in 
  71.                                       local coords rather than new GrafPort with global
  72.                                       coords. Fixed findAgain to actually do something.
  73.                  11/20/93    DHN        Created.
  74. */
  75.  
  76. #include <Folders.h>
  77. #include <GestaltEqu.h>
  78.  
  79. #ifdef USE_CALIB
  80. #include "CALib.h"
  81. #include "CAS_CAUtil.h"
  82. #endif
  83.  
  84. #include "CAS_Globals.h"
  85. #include "CAS_App.h"
  86. #include "CAS_Content.h"
  87. #include "CAS_Doc.h"
  88. #include "CAS_Win.h"
  89. #include "CAS_Event.h"
  90. #include "CAS_Error.h"
  91. #include "CAS_AppleEvent.h"
  92. #include "CAS_Drag.h"
  93. #include "CAS_ToolPalette.h"
  94. #include "CAS_Menu.h"
  95. #include "CAS_MenuItems.h"
  96. #include "CAS_HelpBalloon.h"
  97.  
  98. #include <Finder.h>
  99.  
  100. //----------------------------------------------------------------------
  101. //     local prototypes
  102.  
  103. #if defined(__cplusplus)
  104. extern "C"
  105. {
  106. #endif
  107.  
  108. static pascal void scrollBarActionProc(
  109.     ControlHandle    theControl,
  110.     short            part );
  111. static pascal long App_GrowZoneProc(
  112.     Size    bytesNeeded );
  113. static pascal OSErr App_BgndMouseDownHandler(
  114.     EventRecord        *theEvent,
  115.     short            *bgMouseDownOptions );
  116. static void App_ReadPrefs( void );
  117. static void App_WritePrefs( void );
  118. static void App_GetInfoMenu( void );
  119. static void App_MouseInScrollBars(
  120.     EventRecord        *theEvent,
  121.     WindowPtr        theWindow );
  122. static void App_MouseInDocument(
  123.     EventRecord        *theEvent,
  124.     WindowPtr        theWindow );
  125. static void App_PreferencesMenu( void );
  126. static void App_PrintWindow(
  127.     WindowPtr    theWindow );
  128.  
  129. #if defined(__cplusplus)
  130. }
  131. #endif
  132.  
  133. //----------------------------------------------------------------------
  134.  
  135.  
  136. static pascal void scrollBarActionProc(
  137.     ControlHandle    theControl,
  138.     short            part )
  139. {
  140. DocPtr        theDoc;
  141. short        pageSize, scrollAmt, oldValue, newValue;
  142.  
  143.     if ((theControl == nil) || (part == 0))
  144.         return;
  145.  
  146.     theDoc = (DocPtr)GetCRefCon( theControl );
  147.     oldValue = GetControlValue( theControl );
  148.  
  149.     // $$$$$ this should be a full windowful (depending upon horiz or vert)?
  150.     pageSize = 32;
  151.  
  152.     switch (part)
  153.     {
  154.         case inUpButton: 
  155.             scrollAmt = -1;
  156.             break;
  157.         case inDownButton: 
  158.             scrollAmt = 1;
  159.             break;
  160.         case inPageUp: 
  161.             scrollAmt = -pageSize;
  162.             break;
  163.         case inPageDown: 
  164.             scrollAmt = pageSize;
  165.             break;
  166.  
  167.         default:
  168.             scrollAmt = 0;
  169.             break;
  170.     }
  171.  
  172.     // if it changed, scroll the contents of the window.
  173.     SetControlValue( theControl, oldValue + scrollAmt );
  174.     newValue = GetControlValue( theControl );
  175.     if (oldValue != newValue)
  176.     {
  177.         // determine which scroll bar it was ( h or v) and scroll appropriately.
  178.         if (theControl == theDoc->hScrollBar)
  179.             Win_Scroll( (**theControl).contrlOwner, oldValue - newValue, 0 );
  180.         else
  181.             Win_Scroll( (**theControl).contrlOwner, 0, oldValue - newValue );
  182.             
  183.     }
  184. }
  185.  
  186.  
  187. //---------------------------------------------------------------------------
  188. // App_CloseAll - Close all the windows (by prompting to save changes. Return false 
  189. // if the user cancels or the window fails to close, true if not.)
  190.  
  191. Boolean App_CloseAll( void )
  192. {
  193. WindowPtr    theWindow;
  194. OSErr        theErr;
  195. Boolean        cancelled;
  196. DocPtr        aDoc;
  197. short        i;
  198.  
  199.     for (i = 0; i < kMAXOPENDOCUMENTS; ++i)
  200.     {
  201.         aDoc = gAppPtr->openDocs[i];
  202.         
  203.         if (aDoc != NULL)
  204.         {
  205.             theWindow = Doc_GetWindow (aDoc);
  206.             
  207.             if (theWindow != nil)
  208.             {
  209.                 theErr = Win_Close( theWindow, &cancelled );
  210.                 if (theErr) Error_ReportOperationError (theErr);
  211.             }
  212.             
  213.             if (cancelled) break;
  214.         }
  215.         
  216.     }
  217.  
  218.     /*
  219.     do
  220.     {
  221.         // DAS: changed from FrontWindow() to App_GetFrontDocWindow.
  222.         theWindow = App_GetFrontDocWindow();
  223.         if (theWindow != nil)
  224.         {
  225.             theErr = Win_Close( theWindow, &cancelled );
  226.             if (theErr) Error_ReportOperationError (theErr);
  227.         }
  228.             
  229.     }
  230.     while ((!cancelled) && (!theErr) && (theWindow != nil));
  231.     */
  232.     
  233.     return (!cancelled);
  234. }
  235.  
  236. //---------------------------------------------------------------------------
  237. static pascal long App_GrowZoneProc(
  238.     Size    bytesNeeded )
  239. {
  240. // $$$$$ Can't put up a dialog since we may be in the background.
  241. // What else can we do? Notification Mgr?
  242. //    Dialog_ErrorDialog(itOutOfMemory);
  243.     return 0L;
  244. }
  245.  
  246. //---------------------------------------------------------------------------
  247. // NOT IMPLEMENTED YET
  248. //
  249. // We get called here if our app is clicked while in the background. We check
  250. // to see if the click was in a dragable area. If so, we handle the drag and 
  251. // return EventAlreadyProcessed in bgMouseDownOptions if the drag is to another 
  252. // app, or JustBringToFront if it's in our app. If the click is not in a dragable
  253. // area, we return JustBringToFront. (Do we ever return BringToFrontAndMouseDown?).
  254.  
  255. static pascal OSErr App_BgndMouseDownHandler(
  256.     EventRecord        *theEvent,
  257.     short            *bgMouseDownOptions )
  258. {
  259.     return noErr;
  260. }
  261.  
  262. //---------------------------------------------------------------------------
  263. // load the prefs from our pref file.
  264. static void App_ReadPrefs( void )
  265. {
  266. FSSpec            theSpec;
  267. FInfo            fndrInfo;
  268. tPrefHandle        prefs;
  269. OSErr            theErr;
  270. short            savedRRef;
  271.  
  272.     prefs = nil;
  273.  
  274.     // get name of our pref file
  275.     GetIndString( theSpec.name, kStrings, itOurPrefName );
  276.     theErr =
  277.         FindFolder(
  278.             kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
  279.             &theSpec.vRefNum, &theSpec.parID );
  280.     if (theErr != noErr)
  281.         return;
  282.  
  283.     // remember current resource file
  284.     savedRRef = CurResFile();
  285.  
  286.     gPrefRefNum = FSpOpenResFile( &theSpec, fsRdWrPerm );
  287.     if (gPrefRefNum >= 0)    // if it opened…
  288.     {
  289.     // make sure the type and creator are ours, else change em.
  290.         theErr = FSpGetFInfo( &theSpec, &fndrInfo );
  291.         if (theErr == noErr)
  292.         {
  293.             if ((fndrInfo.fdCreator != kOurFileCreator) || (fndrInfo.fdType != kOurPrefType))
  294.             {
  295.                 fndrInfo.fdCreator = kOurFileCreator;
  296.                 fndrInfo.fdType = kOurPrefType;
  297.                 theErr = FSpSetFInfo( &theSpec, &fndrInfo );
  298.             }
  299.         }
  300.  
  301.         UseResFile( gPrefRefNum );
  302.  
  303.         prefs = (tPrefHandle)Get1Resource( kPrefResType, kPrefResId );
  304.     }
  305.     else
  306.     {
  307.         gPrefRefNum = 0;    // didn't open so init it.
  308.     }
  309.  
  310.     // if we got the prefs and they're the right version, set the pref theDoc from them
  311.     if ((prefs != nil) && ((**prefs).version == kPrefVersion))
  312.     {
  313.         gPrefSampleFlag = (**prefs).prefSampleFlag;
  314.         gCurrentTool = (**prefs).currentTool;
  315.     }
  316.     else    // we didn't get our prefs or they were the wrong version, so init the variables.
  317.     {
  318.         gPrefSampleFlag = false;
  319.         gCurrentTool = 0;
  320.     }
  321.  
  322.     if (gPrefRefNum != 0)
  323.         UseResFile( savedRRef );
  324. }
  325.  
  326. //---------------------------------------------------------------------------
  327.  
  328. static void App_WritePrefs( void )
  329. {
  330. FSSpec            theSpec;
  331. tPrefHandle        prefs;
  332. Handle            tempHandle;
  333. OSErr            theErr;
  334. short            savedRRef;
  335.  
  336.     // if nothing changed
  337.     if (!gPrefChanged)
  338.     {
  339.         // if the pref file was open, close it.
  340.         if (gPrefRefNum != 0)
  341.         {
  342.             CloseResFile( gPrefRefNum );
  343.             gPrefRefNum = 0;
  344.         }
  345.         return;
  346.     }
  347.  
  348.     // remember current resource file
  349.     savedRRef = CurResFile();
  350.  
  351.     // if the pref file did NOT open, try to create one
  352.     if (gPrefRefNum == 0)
  353.     {
  354.         // get name of our pref file
  355.         GetIndString( theSpec.name, kStrings, itOurPrefName );
  356.         theErr =
  357.             FindFolder(
  358.                 kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
  359.                 &theSpec.vRefNum, &theSpec.parID );
  360.         if (theErr != noErr)
  361.             return;
  362.  
  363.         // open failed, so create one
  364.         FSpCreateResFile( &theSpec, kOurFileCreator, kOurPrefType, smRoman );    // use roman script
  365.         // ignore any error here.
  366.  
  367.         // try to open the file
  368.         gPrefRefNum = FSpOpenResFile( &theSpec, fsRdWrPerm );
  369.         // open new file
  370.         // ignore any error here.
  371.     }
  372.  
  373.     // if we got our pref file, save the prefs.
  374.     if (gPrefRefNum != 0)
  375.     {
  376.         UseResFile( gPrefRefNum );
  377.  
  378.         prefs = (tPrefHandle)NewHandleClear( sizeof(tPrefRec) );
  379.  
  380.         // if we got space for the prefs, set them from our theDoc
  381.         if (prefs != nil)
  382.         {
  383.             (**prefs).version            = kPrefVersion;
  384.             (**prefs).prefSampleFlag    = gPrefSampleFlag;
  385.             (**prefs).currentTool        = gCurrentTool;
  386.  
  387.             tempHandle = Get1Resource( kPrefResType, kPrefResId );
  388.             if (tempHandle != nil)        // if a prefs resource existed, remove it.
  389.             {
  390.                 RmveResource( tempHandle );
  391.                 theErr = ResError();
  392.                 if (theErr != noErr)
  393.                     DEBUGSTR( "\pApp_WritePrefs: we have an error after RmveResource" );
  394.             }
  395.  
  396.             // Add the new prefs and flush the file.
  397.             AddResource( (Handle)prefs, kPrefResType, kPrefResId, "\p" );    // add to the file
  398.             theErr = ResError();
  399.             if (theErr != noErr)
  400.                 DEBUGSTR( "\pApp_WritePrefs: error from AddResource" );
  401.             UpdateResFile( gPrefRefNum );                        // update resource file
  402.             theErr = ResError();
  403.             if (theErr != noErr)
  404.                 DEBUGSTR( "\pApp_WritePrefs: error from UpdateResFile" );
  405.             theErr = FlushVol( nil, 0 );                            // flush it out to disk
  406.             if (theErr != noErr)
  407.                 DEBUGSTR( "\pApp_WritePrefs: error from FlushVol" );
  408.         }
  409.  
  410.         CloseResFile( gPrefRefNum );
  411.         gPrefRefNum = 0;
  412.  
  413.         UseResFile( savedRRef );
  414.     }
  415. }
  416.  
  417. //---------------------------------------------------------------------------
  418. // App_Init - does any initialization that the app might need including
  419. // initializing all app specific theDoc.
  420.  
  421. OSErr App_Init( void )
  422. {
  423. OSErr    theErr;
  424. long    response, pictCount, i;
  425.  
  426.     // if we're not running system 7 display a message and quit.
  427.     if (!runningSystem7)
  428.     {
  429.         Error_ShowMessage( kCASMsgNotSystem7 );
  430.         return envNotPresent;        // a logical choice.
  431.     }        // else if we're running earlier than system 7.5, display a message and continue
  432.     else 
  433.     {
  434.         theErr = Gestalt( gestaltSystemVersion, &response );
  435.         if ((theErr != noErr) || (response < 0x0750))
  436.             Error_ShowMessage( kCASMsgLessThan75 );
  437.     }
  438.  
  439.     AE_InitAppleEvents();
  440.  
  441.     gGrowZoneUPP = NewGrowZoneProc( App_GrowZoneProc );
  442.     SetGrowZone( gGrowZoneUPP );
  443.  
  444. #ifdef THINK_C
  445.     // install the drag handlers and the background mouse down handler
  446.     if (hasDragMgr)
  447.         SetBgndMouseHandler( &App_BgndMouseDownHandler );
  448. #endif
  449.  
  450.     gWindowEventFilterUPP = NewModalFilterProc( bWindowEventFilter );
  451.     gWindowEventFilterYDUPP = NewModalFilterYDProc( bWindowEventYDFilter );
  452.     gScrollBarActionUPP = NewControlActionProc( scrollBarActionProc );
  453.     gDragReceiveHandlerUPP = NewDragReceiveHandlerProc( Drag_DragReceiveHandler );
  454.     gDragTrackingHandlerUPP = NewDragTrackingHandlerProc( Drag_DragTrackingHandler );
  455.  
  456. #ifdef USE_CALIB
  457.  
  458.     gDragSendProcUPP = NewDragSendDataProc (Drag_DragSendDataProc);
  459.     // 3/16 RB - For custom save dialog
  460.     gDlgHookYDUPP = NewDlgHookYDProc( CASaveDialogHook );
  461.     
  462. #endif
  463.  
  464.     gSplatsnd = GetResource( 'snd ', 128 );
  465.  
  466.     pictCount = 4;
  467.     for (i=0; i<pictCount; i++)
  468.         gItemPICT[i] = GetPicture( 1000 + i );
  469.  
  470.     App_ReadPrefs();
  471.  
  472.     gCurrentTool = 0;
  473.     (void)ToolPalette_Create( gItemPICT, pictCount, gCurrentTool );
  474.  
  475. #ifdef USE_CALIB
  476.     if (gCALibExists)
  477.     {
  478.         CARegisterFloatingWindow (ToolPalette_VerifyWindow(NULL));
  479.         if (theErr = CAError())
  480.             ;        // handle the error
  481.     }
  482. #endif
  483.  
  484.     gAppPtr = (AppPtr) NewPtr (sizeof (AppRecord));
  485.     gAppPtr->openDocCount = 0;
  486.     
  487.     for (i = 0; i < kMAXOPENDOCUMENTS; ++i)
  488.         gAppPtr->openDocs[i] = NULL;
  489.     
  490.     return noErr;
  491. }
  492.  
  493.  
  494. //---------------------------------------------------------------------------
  495. void App_LogOpenDocument (DocPtr theDoc)
  496. {
  497.     short i = 0;
  498.     
  499.     while (gAppPtr->openDocs[i] != NULL) ++i;
  500.     
  501.     gAppPtr->openDocs[i] = theDoc;
  502.     gAppPtr->openDocCount += 1;
  503.  
  504. }
  505.  
  506. //---------------------------------------------------------------------------
  507. void App_LogCloseDocument (DocPtr theDoc)
  508. {
  509.     short i;
  510.     
  511.     for (i = 0; i < kMAXOPENDOCUMENTS; ++i)
  512.     {
  513.         if (gAppPtr->openDocs[i] == theDoc)
  514.         {
  515.             gAppPtr->openDocs[i] = NULL;
  516.             gAppPtr->openDocCount -= 1;
  517.             break;
  518.         }
  519.     }
  520.     
  521.  
  522. }
  523.  
  524. //---------------------------------------------------------------------------
  525. //     App_SetUpMenus - insert any application menus
  526.  
  527. void App_SetUpMenus( void )
  528. {
  529. MenuHandle    theMenu;
  530.  
  531.     theMenu = GetMenu( kToolMenu );
  532.     if (theMenu != nil)
  533.         InsertMenu( theMenu, 0 );
  534.  
  535. }
  536.  
  537. //---------------------------------------------------------------------------
  538. //     appQuit - clean up before quitting. Reverse any actions from App_Init.
  539.  
  540. void App_Quit( void )
  541. {
  542.     App_WritePrefs();                // save our prefs
  543.  
  544.     ToolPalette_Dispose( nil );
  545.  
  546.     DisposeRoutineDescriptor( gGrowZoneUPP );
  547.  
  548.     DisposeRoutineDescriptor( gWindowEventFilterUPP );
  549.     DisposeRoutineDescriptor( gWindowEventFilterYDUPP );
  550.     DisposeRoutineDescriptor( gScrollBarActionUPP );
  551.     DisposeRoutineDescriptor( gDragReceiveHandlerUPP );
  552.     DisposeRoutineDescriptor( gDragTrackingHandlerUPP );
  553.  
  554.     AE_DisposeAppleEvents();
  555.     
  556.     DisposePtr ((Ptr) gAppPtr);
  557.     
  558. }
  559.  
  560. //---------------------------------------------------------------------------
  561. // App_Idle - called during idle time when there is no event
  562.  
  563. void App_Idle(
  564.     WindowPtr    theWindow )
  565. {
  566. // NOTE: remove the #ifdef if any working code is placed here!
  567.  
  568. #pragma unused (theWindow)
  569.  
  570. #if 0
  571. GrafPtr        savePort;
  572.  
  573.     GetPort( &savePort );
  574.  
  575.     if (theWindow != nil)
  576.         SetPort( theWindow );
  577.  
  578.     // Do any idle work here:
  579.     //        call TEIdle if using Text Edit.
  580.  
  581.     SetPort( savePort );
  582. #endif
  583. }
  584.  
  585. //---------------------------------------------------------------------------
  586. // Adjust the cursor depending on theLoc (global coords). Copy the region in 
  587. // which the cursor will not change into the existing region theRgn. Assume
  588. // that we are not in the background when we get called here.
  589.  
  590. void App_AdjustCursor(
  591.     WindowPtr    theWindow,
  592.     Point        theLoc,
  593.     RgnHandle    theRgn )
  594. {
  595. DocPtr        theDoc;
  596. RgnHandle    arrowRgn, itemsRgn;
  597. WindowPtr    toolWindow, appWindow;
  598. GrafPtr        savePort;
  599. Rect        theRect;
  600. ElemPtr        elem;
  601. short        elemPart;
  602. CursPtr        defaultCursor;
  603. Point        localPoint;
  604.  
  605.     if (gCurrentTool == 0)
  606.         defaultCursor = &qd.arrow;
  607.     else
  608.         defaultCursor = *GetCursor( crossCursor );
  609.         
  610.     theDoc = nil;
  611.  
  612.     arrowRgn = NewRgn();
  613.     //SetRectRgn( arrowRgn, -0x7FFE, -0x7FFE, 0x7FFE, 0x7FFE );
  614.     SetRectRgn( arrowRgn, 0, 0, 1, 1 );
  615.  
  616.     // make sure there is a window and it's ours
  617.     if (!Win_IsAppWindow( theWindow ))
  618.     {
  619.         CopyRgn( arrowRgn, theRgn );
  620.         SetCursor( &qd.arrow );
  621.     }
  622.     else
  623.     {
  624.         // Check if the mouse is in the ToolPalette
  625.         toolWindow = ToolPalette_GetWindow();
  626.  
  627.         CopyRgn( ((WindowPeek)theWindow)->strucRgn, arrowRgn );
  628.         InsetRgn( arrowRgn, 16, 16 );
  629.  
  630.         // is the mouse in the tool pallete?
  631.         if (PtInRgn( theLoc, ((WindowPeek)toolWindow)->strucRgn ))
  632.         {
  633.             SetCursor( &qd.arrow );
  634.             CopyRgn( ((WindowPeek)toolWindow)->strucRgn, theRgn );
  635.         }
  636.         else if (PtInRgn( theLoc, ((WindowPeek)theWindow)->strucRgn))
  637.         {
  638.             // get the contentRect into theRgn.
  639.  
  640.             // get the window theDoc.
  641.             theDoc = (DocPtr)GetWRefCon( theWindow );
  642.  
  643.             GetPort( &savePort );
  644.             SetPort( theWindow );
  645.  
  646.             localPoint = theLoc;
  647.             GlobalToLocal( &localPoint );
  648.  
  649.             elemPart = ElemColl_FindElem(theDoc->contentColl, localPoint, &elem);
  650.             
  651.             if (elem)
  652.             {
  653.                 if (Elem_GetSelected(elem))
  654.                 {
  655.                     if (elemPart == inElement)
  656.                     {
  657.                         SetCursor( *GetCursor( handCursor ));
  658.                         Elem_GetLocatedRect (elem, &theRect);
  659.                         Doc_RectDocToGlobal( theDoc, &theRect );
  660.                         RectRgn( theRgn, &theRect );
  661.  
  662.                     }
  663.                     if ((elemPart == inCornerResize) ||
  664.                         (elemPart == inEdgeResize))
  665.                     {
  666.                         SetCursor( &qd.arrow );
  667.                         CopyRgn (elem->selRgn.cornerHandleRgn, theRgn);
  668.                         UnionRgn (elem->selRgn.edgeHandleRgn, theRgn, theRgn);
  669.                     }
  670.                 }
  671.                 else
  672.                 {
  673.                     SetCursor( &qd.arrow );
  674.                     Elem_GetLocatedRect (elem, &theRect);
  675.                     Doc_RectDocToGlobal( theDoc, &theRect );
  676.                     RectRgn( theRgn, &theRect );
  677.  
  678.                 }
  679.  
  680.             }
  681. #ifdef USE_CALIB
  682.         
  683.             else if (CAActiveBorderContainsPoint (theLoc))
  684.             {
  685.                 CopyRgn( ((WindowPeek)theWindow)->strucRgn, theRgn );
  686.                 SetCursor( *GetCursor( handCursor ));
  687.             }
  688.             
  689. #endif
  690.             else if (!PtInRgn( theLoc, arrowRgn ))
  691.             {
  692.                 // mouse Rgn is the non-content part of the window
  693.                 CopyRgn( ((WindowPeek)theWindow)->strucRgn, theRgn );
  694.                 DiffRgn( theRgn, arrowRgn, theRgn );
  695.  
  696.                 // Subtract the ToolPalette Rgn
  697.                 toolWindow = ToolPalette_GetWindow();
  698.                 DiffRgn( theRgn, ((WindowPeek)toolWindow)->strucRgn, theRgn );
  699.  
  700.                 SetCursor( &qd.arrow );
  701.             }
  702.             else
  703.             {
  704.                 itemsRgn = NewRgn();
  705.  
  706.                 RectRgn( theRgn, &theDoc->contentRect );
  707.                 RgnLocalToGlobal( theRgn );
  708.  
  709.                 // Subtract the ToolPalette Rgn
  710.                 toolWindow = ToolPalette_GetWindow();
  711.                 if (toolWindow != nil)
  712.                     DiffRgn( theRgn, ((WindowPeek)toolWindow)->strucRgn, theRgn );
  713.  
  714.                 Doc_CalcContentElemRgn( theDoc, itemsRgn );
  715.                 Doc_RgnDocToGlobal( theDoc, itemsRgn );
  716.  
  717.                 DiffRgn( theRgn, itemsRgn, theRgn );
  718.  
  719.                 DisposeRgn( itemsRgn );
  720.                 SetCursor (defaultCursor);
  721.                 
  722.             }
  723.  
  724.             SetPort( savePort );
  725.         }
  726.         else    // Mouse is not in the front doc or the tool palette window
  727.         {
  728.             SetCursor( &qd.arrow );
  729.             CopyRgn( LMGetGrayRgn(), theRgn );
  730.  
  731.             // Subtract the front doc window Rgn and the tool palette Rgn
  732.             appWindow = App_GetFrontDocWindow();
  733.             DiffRgn( theRgn, ((WindowPeek)appWindow)->strucRgn, theRgn );
  734.  
  735.             toolWindow = ToolPalette_GetWindow();
  736.             DiffRgn( theRgn, ((WindowPeek)toolWindow)->strucRgn, theRgn );
  737.         }
  738.     }
  739.  
  740.     DisposeRgn( arrowRgn );
  741. }
  742.  
  743. //---------------------------------------------------------------------------
  744.  
  745. void App_AdjustMenus(
  746.     WindowPtr    theWindow )
  747. {
  748. Boolean        selectionExists;
  749. DocPtr        theDoc;
  750. MenuHandle    targetMenuH;
  751. long        result, scrapOffset, i;
  752. Boolean        bWin_IsAppWindow;
  753.  
  754.     bWin_IsAppWindow = Win_IsAppWindow( theWindow );
  755.     if (bWin_IsAppWindow)
  756.     {
  757.         theDoc = (DocPtr)GetWRefCon( theWindow );
  758.         selectionExists = Doc_SelectionExists( theDoc );
  759.     
  760.     }
  761.     else
  762.     {
  763.         theDoc = nil;
  764.         selectionExists = false;
  765.     }
  766.  
  767.     // enable the save menu if there is a doc and (it has changed or never been saved).
  768.     targetMenuH = GetMenuHandle( kFileMenu );
  769.     mhSetMenuItem(bWin_IsAppWindow && (Doc_GetDirty( theDoc )
  770.                     || (theDoc->fileRefNum <= 0)), targetMenuH, kSaveItem );
  771.  
  772.     // SaveAs
  773.     mhSetMenuItem(bWin_IsAppWindow, targetMenuH, kSaveAsItem );
  774.     //mhSetMenuItem(false, targetMenuH, kSaveAsItem );
  775.  
  776.     // Close
  777.     mhSetMenuItem(true, targetMenuH, kCloseItem );
  778.  
  779. #ifdef USE_CALIB
  780.     // enable the GetInfo menu if there is a doc
  781.     mhSetMenuItem( bWin_IsAppWindow, targetMenuH, kDocumentInfoItem );
  782. #else
  783.     // enable the GetInfo menu if there is a doc
  784.     mhSetMenuItem( bWin_IsAppWindow, targetMenuH, kGetInfoItem );
  785. #endif
  786.     
  787.     result = GetScrap( nil, kPICTType, &scrapOffset );
  788.     
  789. #ifdef USE_CALIB
  790.  
  791.     if (gCALibExists)
  792.     {
  793.  
  794.         result = ((InfoScrap())->scrapSize > 0) ? 1 : -1;
  795.         
  796. #if !CALIB_FRIENDLY_DATA_TRANSFER
  797. #if 0
  798.         // Check if the OpenDoc clipboard has content
  799.         if (!result)
  800.         {
  801.             CADocumentRef    clipDoc;
  802.             
  803.             clipDoc = CAGetClipboardDocument(theDoc, kCACloneCopy);
  804.             if (CAGetSize(clipDoc))
  805.                 result = true;
  806.             CACloseDocument (clipDoc);
  807.         }
  808. #endif
  809. #endif
  810.  
  811.         if (!bWin_IsAppWindow)
  812.         {
  813.             mhSetMenuItem( bWin_IsAppWindow, targetMenuH, kInsertItem );
  814.         }
  815.         else
  816.         {
  817.                     
  818.             if (theDoc->partDocRef == gFocusDocument)
  819.             {
  820.        
  821.                 mhSetMenuItem( bWin_IsAppWindow, targetMenuH, kInsertItem );
  822.                 
  823.                 mhSetMenuItem( (Doc_GetSelectedFrameCount(theDoc) == 1), targetMenuH, 
  824.                               kOpenSelectionItem);
  825.                 
  826.                    targetMenuH = GetMenuHandle( kEditMenu );
  827.     
  828.                  mhSetMenuItem( selectionExists && !theDoc->fileLocked, targetMenuH, kCutItem );
  829.                 mhSetMenuItem( selectionExists, targetMenuH, kCopyItem );
  830.                 mhSetMenuItem( selectionExists && !theDoc->fileLocked, targetMenuH, kClearItem );
  831.                 mhSetMenuItem( (result > 0) && (theDoc != nil) && !theDoc->fileLocked, targetMenuH, kPasteItem );
  832.             
  833.                 // enable the selectAll menu if there are any items in the document.
  834.                 mhSetMenuItem( bWin_IsAppWindow && (ElemColl_GetCount (theDoc->contentColl) > 0), targetMenuH, kSelectAllItem );
  835.         
  836.                 mhSetMenuItem( (Doc_GetSelectedFrameCount(theDoc) == 1), targetMenuH, 
  837.                              kPartInfoItem);
  838.                  
  839.                  mhSetMenuItem( false, targetMenuH, kViewInWindowItem );
  840.  
  841.                 // check the current tool menu item.
  842.                 targetMenuH = GetMenuHandle( kToolMenu );
  843.                 if (targetMenuH)
  844.                 {
  845.                     for (i=1; i <= kTool4; i++)
  846.                         CheckItem( targetMenuH, i, i == gCurrentTool+1 );
  847.                 }
  848.             
  849.             }
  850.             else
  851.             {
  852.             
  853.                 targetMenuH = GetMenuHandle( kEditMenu );
  854.                  mhSetMenuItem( true, targetMenuH, kViewInWindowItem );
  855.  
  856.             }
  857.             
  858.         }
  859.     }
  860.     else
  861.     {
  862.            targetMenuH = GetMenuHandle( kEditMenu );
  863.  
  864.         mhSetMenuItem( selectionExists && !theDoc->fileLocked, targetMenuH, kCutItem );
  865.         mhSetMenuItem( selectionExists, targetMenuH, kCopyItem );
  866.         mhSetMenuItem( (result > 0) && (theDoc != nil) && !theDoc->fileLocked, targetMenuH, kPasteItem );
  867.         mhSetMenuItem( selectionExists && !theDoc->fileLocked, targetMenuH, kClearItem );
  868.     
  869.         // enable the selectAll menu if there are any items in the document.
  870.         mhSetMenuItem( bWin_IsAppWindow && (ElemColl_GetCount (theDoc->contentColl) > 0), targetMenuH, kSelectAllItem );
  871.     
  872.         // check the current tool menu item.
  873.         targetMenuH = GetMenuHandle( kToolMenu );
  874.         for (i=1; i <= kTool4; i++)
  875.             CheckItem( targetMenuH, i, i == gCurrentTool+1 );
  876.     }
  877.     
  878. #else
  879.  
  880.     mhSetMenuItem( selectionExists && !theDoc->fileLocked, targetMenuH, kCutItem );
  881.     mhSetMenuItem( selectionExists, targetMenuH, kCopyItem );
  882.     mhSetMenuItem( (result > 0) && (theDoc != nil) && !theDoc->fileLocked, targetMenuH, kPasteItem );
  883.     mhSetMenuItem( selectionExists && !theDoc->fileLocked, targetMenuH, kClearItem );
  884.  
  885.     // enable the selectAll menu if there are any items in the document.
  886.     mhSetMenuItem( bWin_IsAppWindow && (ElemColl_GetCount (theDoc->contentColl) > 0), targetMenuH, kSelectAllItem );
  887.  
  888.     // check the current tool menu item.
  889.     targetMenuH = GetMenuHandle( kToolMenu );
  890.     for (i=1; i <= kTool4; i++)
  891.         CheckItem( targetMenuH, i, i == gCurrentTool+1 );
  892.  
  893. #endif
  894.  
  895. }
  896.  
  897. //---------------------------------------------------------------------------
  898.  
  899. static void App_GetInfoMenu( void )
  900. {
  901. GrafPtr        savePort;
  902. DialogPtr    theDialog;
  903. WindowPtr    aWindow;
  904. DocPtr        theDoc;
  905. Str63        countStr;
  906. Str63        selectedCountStr;
  907. Str63        docTitle;
  908. long        selectedItemCount, i;
  909.  
  910. #if 0
  911.     GetPort( &savePort );
  912.     aWindow = App_GetFrontDocWindow();
  913.  
  914.     theDoc = (DocPtr)GetWRefCon( aWindow );
  915.  
  916.     NumToString( ElemColl_GetCountOfType (theDoc->contentColl, kPICTType), countStr );
  917.  
  918.     selectedItemCount = 0;
  919.     for (i=0; i<ElemColl_GetCount (theDoc->contentColl); ++i)
  920.         if (theDoc->itemArray[i].selected)
  921.             ++selectedItemCount;
  922.  
  923.     NumToString( selectedItemCount, selectedCountStr );
  924.  
  925.     Dialog_CenterDLOGonFrontWindow( kGetInfoDialog );
  926.     theDialog = GetNewDialog( kGetInfoDialog, nil, nil /* INFRONT */ );
  927.  
  928.     if (theDialog == nil)
  929.         return;
  930.  
  931. #ifdef USE_CALIB
  932.     if (gCALibExists)
  933.     {
  934.         if (!CARequestModalFocus( (WindowPtr)aWindow ))
  935.         {
  936.             DisposeDialog( theDialog );
  937.             return;
  938.         }
  939.     }
  940. #endif
  941.  
  942.     SetPort( theDialog );
  943.  
  944. //    ( void )Win_CenterWindowOnParentWindow( (WindowPtr)theDialog );
  945.     Dialog_OutlineButton( theDialog, 1 );
  946.     Dialog_SetTextItem( theDialog, 5, countStr );
  947.     Dialog_SetTextItem( theDialog, 6, selectedCountStr );
  948.  
  949.     GetWTitle( (WindowPtr)theDialog, docTitle );
  950.     Dialog_SetTextItem( theDialog, 4, docTitle );
  951.  
  952.     App_ForceActivateFrontWindow( false );
  953.     SelectWindow( (WindowPtr)theDialog );
  954.  
  955.     iOKITEM = iCancelITEM = kOKButton;
  956.     do
  957.     {
  958.         ModalDialog( gWindowEventFilterUPP, &itemHit );
  959.     }
  960.     while (itemHit != kOKButton);
  961.  
  962. #ifdef USE_CALIB
  963.     if (gCALibExists)
  964.     {
  965.         OSErr        theErr;
  966.  
  967.         CARelinquishModalFocus( (WindowPtr)aWindow );
  968.         if (theErr = CAError())
  969.             ;        // handle the error
  970.     }
  971. #endif
  972.  
  973.     DisposeDialog( theDialog );
  974.     App_ForceActivateFrontWindow( true );
  975.  
  976.     SetPort( savePort );
  977.     
  978. #endif
  979. }
  980.  
  981. //---------------------------------------------------------------------------
  982. // Handle application specific file menu items here.
  983.  
  984. void App_HandleFileMenu(
  985.     short        menuItem,
  986.     WindowPtr    theWindow )
  987. {
  988. #pragma unused (theWindow)
  989.  
  990.     switch (menuItem)
  991.     {
  992. #ifdef USE_CALIB
  993.         case kDocumentInfoItem:
  994. #else
  995.         case kGetInfoItem:
  996. #endif
  997.             App_GetInfoMenu();
  998.             break;
  999.  
  1000.         default:
  1001.             break;
  1002.     }
  1003. }
  1004.  
  1005. //---------------------------------------------------------------------------
  1006. // Handle application specific edit menu items here.
  1007.  
  1008. void App_HandleEditMenu(
  1009.     short        menuItem,
  1010.     WindowPtr    theWindow )
  1011. {
  1012. #pragma unused (theWindow)
  1013.  
  1014.     switch (menuItem)
  1015.     {
  1016.         case kPreferencesItem:
  1017.             App_PreferencesMenu();
  1018.             break;
  1019.  
  1020.         default:
  1021.             break;
  1022.     }
  1023. }
  1024.  
  1025. //---------------------------------------------------------------------------
  1026. // Handle application specific edit menu items here.
  1027.  
  1028. void App_HandleToolMenu(
  1029.     short        menuItem,
  1030.     WindowPtr    theWindow )
  1031. {
  1032. #pragma unused (theWindow)
  1033.  
  1034.     switch (menuItem)
  1035.     {
  1036.         case kTool1:
  1037.         case kTool2:
  1038.         case kTool3:
  1039.         case kTool4:
  1040.             gCurrentTool = menuItem - 1;
  1041.             ToolPalette_SetSelectedCell( nil, gCurrentTool );
  1042.             gPrefChanged = true;
  1043.             break;
  1044.  
  1045.         default:
  1046.             break;
  1047.     }
  1048. }
  1049.  
  1050. //---------------------------------------------------------------------------
  1051. // Handle application specific menus & items here.
  1052.  
  1053. void App_HandleMenu(
  1054.     long        mSelect,
  1055.     WindowPtr    theWindow )
  1056. {
  1057. short    menuID;
  1058.  
  1059.     menuID = HiWord( mSelect );
  1060.     switch (menuID)
  1061.     {
  1062.         case kToolMenu:
  1063.             App_HandleToolMenu( mSelect, theWindow );
  1064.             break;
  1065.  
  1066.         default:
  1067.             break;
  1068.     }
  1069. }
  1070.  
  1071. //---------------------------------------------------------------------------
  1072. //    do anything that needs to be done when our window is activated.
  1073.  
  1074. void App_Activate(
  1075.     WindowPtr    theWindow )
  1076. {
  1077. DocPtr    theDoc;
  1078.  
  1079.     // if there is no window or it's not our window, get out.
  1080.     if (!Win_IsAppWindow( theWindow ))
  1081.         return;
  1082.  
  1083.     theDoc = (DocPtr)GetWRefCon( theWindow );
  1084.     SetPort(theWindow );
  1085.  
  1086. #ifdef USE_CALIB
  1087.     CAUtil_InvalSelRgns (theDoc);
  1088. #endif
  1089.  
  1090.     // This is the hiding the scroll bars method.
  1091.     if (theDoc)
  1092.     {
  1093.         ShowControl( theDoc->hScrollBar );
  1094.         ShowControl( theDoc->vScrollBar );
  1095.     }
  1096.  
  1097.     HiliteWindow (theWindow, true);
  1098.     DrawGrowIcon( theWindow );
  1099.     
  1100. }
  1101.  
  1102. //---------------------------------------------------------------------------
  1103. //    do anything that needs to be done when our window is deactivated.
  1104.  
  1105. void App_Deactivate(
  1106.     WindowPtr    theWindow )
  1107. {
  1108. GrafPtr        savePort;
  1109. DocPtr        theDoc;
  1110.  
  1111.     // if there is no window or it's not our window, get out.
  1112.     if (!Win_IsAppWindow( theWindow ))
  1113.         return;
  1114.  
  1115.     // NOTE: during window creation, the new window shows up in the 
  1116.     // window list but hasn't been fully initialized by us yet, so
  1117.     // theDoc comes up NIL.
  1118.     theDoc = (DocPtr)GetWRefCon( theWindow );
  1119.  
  1120. #ifdef USE_CALIB
  1121.  
  1122.     GetPort( &savePort );
  1123.     SetPort( theWindow );
  1124.     CAUtil_InvalSelRgns (theDoc);
  1125.     SetPort( savePort );
  1126.  
  1127. #endif
  1128.  
  1129.     // This is the hiding the scroll bars method.
  1130.     if (theDoc)
  1131.     {
  1132.         HideControl( theDoc->hScrollBar );
  1133.         HideControl( theDoc->vScrollBar );
  1134.     }
  1135.  
  1136.     HiliteWindow (theWindow, false);
  1137.     DrawGrowIcon( theWindow );
  1138.     
  1139. }
  1140.  
  1141. //---------------------------------------------------------------------------
  1142. // handle resume event.
  1143.  
  1144. void App_Resume( void )
  1145. {
  1146.     // set the sleep time short so we get called often
  1147.     // but not so short that other apps don't get time.
  1148.     gSleepTime = GetCaretTime();
  1149.  
  1150.     // activate our front window.
  1151.     // DAS: changed from FrontWindow() to App_GetFrontDocWindow()
  1152.     App_Activate( App_GetFrontDocWindow() );
  1153. }
  1154.  
  1155. //---------------------------------------------------------------------------
  1156. // handle suspend event.
  1157.  
  1158. void App_Suspend( void )
  1159. {
  1160.     WindowPtr    frontWindow;
  1161.     
  1162.     // set the sleep time long since we don't need to be called 
  1163.     // very often when we're in the background.
  1164.     gSleepTime = 0x7FFFFFFF;
  1165.  
  1166.     // deactivate our front window.
  1167.     // DAS: changed from FrontWindow() to App_GetFrontDocWindow()
  1168.     
  1169.     #ifdef USE_CALIB
  1170.     #if !CALIB_FRIENDLY_DATA_TRANSFER
  1171.         CAExportClipboard ();
  1172.     #endif
  1173.     #endif
  1174.     
  1175.     frontWindow = App_GetFrontDocWindow();
  1176.     
  1177.     if (Win_IsAppWindow( frontWindow ))
  1178.     {
  1179.     
  1180.         App_Deactivate(frontWindow);
  1181.         
  1182.     }
  1183.     
  1184. }
  1185.  
  1186. //---------------------------------------------------------------------------
  1187. //    draw our window's contents
  1188.  
  1189. void App_Update(
  1190.     WindowPtr    theWindow )
  1191. {
  1192. RgnHandle    saveClip;
  1193. DocPtr        theDoc;
  1194. Rect        theRect;
  1195. GrafPtr        savePort;
  1196. Point        saveOrigin;
  1197. short        baseH, baseV;
  1198.  
  1199.     // if there is no window or it's not ours, get out.
  1200.     if (!Win_IsAppWindow( theWindow ))
  1201.         return;
  1202.  
  1203.     theDoc = (DocPtr)GetWRefCon( theWindow );
  1204.  
  1205.     GetPort( &savePort );
  1206.     SetPort( theWindow );
  1207.  
  1208.     // save the ClipRgn
  1209.     saveClip = NewRgn();
  1210.     GetClip( saveClip );
  1211.  
  1212.     DrawGrowIcon( theWindow );
  1213.     DrawControls( theWindow );
  1214.  
  1215.     // move the coordinate system according to the scroll bars.
  1216.     GetOrigin( &saveOrigin );
  1217.     Doc_SetGrafOrigin( theDoc );
  1218.  
  1219. #ifndef USE_CALIB
  1220.     // clip to everything but the scroll bars and grow box.
  1221.     theRect = theDoc->contentRect;
  1222.     baseH = GetControlValue( theDoc->hScrollBar );
  1223.     baseV = GetControlValue( theDoc->vScrollBar );
  1224.     OffsetRect( &theRect, baseH, baseV );
  1225.     ClipRect( &theRect );
  1226. #else
  1227. {
  1228.     // For the CALib version also add the area used by
  1229.     // frames to the clip
  1230.     
  1231.     //clipRgn = NewRgn();
  1232.     theRect = theDoc->contentRect;
  1233.     baseH = GetControlValue( theDoc->hScrollBar );
  1234.     baseV = GetControlValue( theDoc->vScrollBar );
  1235.     OffsetRect( &theRect, baseH, baseV );
  1236.     ClipRect( &theRect );
  1237.  
  1238. }
  1239. #endif
  1240.  
  1241.     EraseRect( &theDoc->contentRect );
  1242.  
  1243.     // draw the content area here.
  1244.     Doc_Draw( theDoc );
  1245.  
  1246.     // set the coordinate system back.
  1247.     // put the clipRgn back and get rid of the saved rgn.
  1248.     SetOrigin( saveOrigin.h, saveOrigin.v );
  1249.     SetClip( saveClip );
  1250.     DisposeRgn( saveClip );
  1251.  
  1252.     SetPort( savePort );
  1253.  
  1254. }
  1255.  
  1256. //----------------------------------------------------------------------
  1257. // Called after the window is moved. Assume the window is a doc window.
  1258.  
  1259. void App_Drag(
  1260.     WindowPtr    theWindow )
  1261. {
  1262.     Win_AdjustStdState( theWindow );
  1263. }
  1264.  
  1265. //----------------------------------------------------------------------
  1266. // Called after the window is resized.
  1267.  
  1268. void App_Grow(
  1269.     WindowPtr    theWindow )
  1270. {
  1271. //    EraseRect(&theWindow->portRect);
  1272. //    InvalRect(&theWindow->portRect);
  1273.     Win_Adjust( theWindow );
  1274.     Win_AdjustStdState( theWindow );
  1275. }
  1276.  
  1277. //----------------------------------------------------------------------
  1278. // Called after the window is zoomed in (smaller).
  1279.  
  1280. void App_ZoomIn(
  1281.     WindowPtr    theWindow )
  1282. {
  1283.     Win_CheckWindowPosition( theWindow );
  1284.     Win_AdjustStdState( theWindow );
  1285.     Win_Adjust( theWindow );
  1286. }
  1287.  
  1288. //----------------------------------------------------------------------
  1289. // Called after the window is zoomed out (larger).
  1290.  
  1291. void App_ZoomOut(
  1292.     WindowPtr    theWindow )
  1293. {
  1294.     Win_CheckWindowPosition( theWindow );
  1295.     Win_AdjustStdState( theWindow );
  1296.     Win_Adjust( theWindow );
  1297. }
  1298.  
  1299. //----------------------------------------------------------------------
  1300. //    handle key presses.
  1301.  
  1302. void App_Key(
  1303.     WindowPtr    theWindow,
  1304.     short        code,
  1305.     short        modifiers )
  1306. {
  1307. #pragma unused (modifiers)
  1308.  
  1309. DocPtr        theDoc;
  1310. short        key, ctlValue;
  1311.  
  1312.     // if there is no window or it's not ours, get out.
  1313.     if (!Win_IsAppWindow( theWindow ))
  1314.         return;
  1315.  
  1316.     theDoc = (DocPtr)GetWRefCon(theWindow);
  1317.  
  1318.     key = (code & keyCodeMask) >> 8;
  1319.     code = code & charCodeMask;
  1320.  
  1321.     // $$$$$ if we don't update the window before scrolling it, we'll get
  1322.     // left over white space from the balloon.
  1323.     hideBalloons(theWindow);        // reset the balloons since the selection could be moving.
  1324.  
  1325.     switch (code)
  1326.     {
  1327.         case kHome:
  1328.             ctlValue = GetControlValue( theDoc->vScrollBar );
  1329.             SetControlValue( theDoc->vScrollBar, GetControlMinimum( theDoc->vScrollBar ) );
  1330.             Win_Scroll( theWindow, 0, ctlValue - GetControlMinimum( theDoc->vScrollBar ) );
  1331.             break;
  1332.  
  1333.         case kEnd:
  1334.             ctlValue = GetControlValue( theDoc->vScrollBar );
  1335.             SetControlValue( theDoc->vScrollBar, GetControlMaximum( theDoc->vScrollBar ) );
  1336.             Win_Scroll( theWindow, 0, ctlValue - GetControlMaximum( theDoc->vScrollBar ) );
  1337.             break;
  1338.  
  1339.         case kPageUp:
  1340.         case kPageDown: 
  1341.             scrollBarActionProc( theDoc->vScrollBar, (code == kPageUp) ? inPageUp : inPageDown );
  1342.             break;
  1343.  
  1344.         case kLeftArrow:
  1345.             Doc_MoveSelection( theDoc, -1, 0 );
  1346.             break;
  1347.  
  1348.         case kRightArrow:
  1349.             Doc_MoveSelection( theDoc, 1, 0 );
  1350.             break;
  1351.  
  1352.         case kUpArrow:
  1353.             Doc_MoveSelection( theDoc, 0, -1 );
  1354.             break;
  1355.  
  1356.         case kDownArrow:
  1357.             Doc_MoveSelection( theDoc, 0, 1 );
  1358.             break;
  1359.  
  1360.         case kDelete:
  1361.             Doc_ClearMenu( theDoc );        // clear the selection.
  1362.             break;
  1363.  
  1364.         case kEscape:
  1365.             // clear and escape share same char code so check the key code
  1366.             if (key == 0x47)    // if there is a selection, clear maps to clear menu.
  1367.                 Doc_ClearMenu( theDoc );            // clear the selection.
  1368.             break;
  1369.  
  1370.         case kEnter:    // ignore the enter key.
  1371.             break;
  1372.  
  1373.         case kFunctionKeys:
  1374.             // function keys (F1-F4) all share the same char code so check the key.
  1375.             switch (key)
  1376.             {
  1377.                 case 0x7A:
  1378.                     Doc_UndoMenu( theDoc );
  1379.                     break;
  1380.                 case 0x78:
  1381.                     Doc_CutMenu( theDoc );
  1382.                     break;
  1383.                 case 0x63:
  1384.                     Doc_CopyMenu( theDoc, false );
  1385.                     break;
  1386.                 case 0x76:
  1387.                     Doc_PasteMenu( theDoc );
  1388.                     break;
  1389.  
  1390.                 default:
  1391.                     break;
  1392.             }
  1393.             break;
  1394.  
  1395.         default:        // all other key strokes are handled here.
  1396.             break;
  1397.     }
  1398. }
  1399.  
  1400. //----------------------------------------------------------------------
  1401. //    handle command key presses.
  1402.  
  1403. void App_CmdKey(
  1404.     WindowPtr    theWindow,
  1405.     short        code,
  1406.     short        modifiers )
  1407. {
  1408. #pragma unused (modifiers)
  1409.  
  1410.     // if there is no window or it's not ours, get out.
  1411.     if (!Win_IsAppWindow( theWindow ))
  1412.         return;
  1413.  
  1414.     // strip off any keyCode.
  1415.     code = code & charCodeMask;
  1416.  
  1417.     // $$$$$ if we don't update the window before scrolling it, we'll get
  1418.     // left over white space from the balloon.
  1419.     // reset the balloons since the selection could be moving.
  1420.     hideBalloons( theWindow );
  1421.  
  1422.     switch (code)
  1423.     {
  1424.         case kLeftArrow:
  1425.             break;
  1426.         case kRightArrow:
  1427.             break;
  1428.         case kUpArrow:
  1429.             break;
  1430.         case kDownArrow:
  1431.             break;
  1432.  
  1433.         default:
  1434.             break;
  1435.     }
  1436. }
  1437.  
  1438. //---------------------------------------------------------------------------
  1439. // called when there is a mouse down in the scroll bars of an app window.
  1440.  
  1441. static void App_MouseInScrollBars(EventRecord *theEvent, WindowPtr theWindow)
  1442. {
  1443. Point            thePt;
  1444. short            thePart, newValue, oldValue;
  1445. ControlHandle    theControl;
  1446. DocPtr            theDoc;
  1447.  
  1448.     theDoc = (DocPtr)GetWRefCon( theWindow );
  1449.  
  1450.     thePt = theEvent->where;
  1451.     GlobalToLocal( &thePt );
  1452.  
  1453.     // find the control clicked on
  1454.     thePart = FindControl( thePt, theWindow, &theControl );
  1455.     if ((theControl != nil) && (thePart != 0))
  1456.     {
  1457.         // click on scroll bar
  1458.         if ((theControl == theDoc->hScrollBar) || (theControl == theDoc->vScrollBar))
  1459.         {
  1460.             if (thePart == inThumb)                                    // track the thumb
  1461.             {
  1462.                 // get previous setting
  1463.                 oldValue = GetControlValue( theControl );
  1464.                 thePart = TrackControl( theControl, thePt, nil );
  1465.                 newValue = GetControlValue( theControl );
  1466.  
  1467.                 // if the scroll bar changed, scroll the window contents.
  1468.                 if (oldValue != newValue)
  1469.                 {
  1470.                     // determine which scroll bar it was ( h or v) and scroll appropriately.
  1471.                     if (theControl == theDoc->hScrollBar)
  1472.                         Win_Scroll( theWindow, oldValue - newValue, 0 );
  1473.                     else
  1474.                         Win_Scroll( theWindow, 0, oldValue - newValue );
  1475.                 }
  1476.             }
  1477.             else
  1478.             {
  1479.                 /* track arrows or page up/down */
  1480.                 thePart = TrackControl( theControl, thePt, gScrollBarActionUPP );
  1481.             }
  1482.         }
  1483.     }
  1484. }
  1485.  
  1486. //---------------------------------------------------------------------------
  1487. // called when there is a mouse down in document area of an app window.
  1488.  
  1489. static void App_MouseInDocument(
  1490.     EventRecord        *theEvent,
  1491.     WindowPtr        theWindow )
  1492. {
  1493. Point            thePt;
  1494. RgnHandle        saveClip;
  1495. GrafPtr            savePort;
  1496. Rect            theRect;
  1497. Point            docPt;
  1498. DocPtr            theDoc;
  1499. Point            saveOrigin;
  1500. ElemPtr            elem;
  1501. short            elemPart;
  1502.  
  1503.     theDoc = (DocPtr)GetWRefCon( theWindow );
  1504.  
  1505.     GetPort (&savePort);
  1506.     SetPort (theWindow);
  1507.     
  1508.     thePt = theEvent->where;
  1509.     GlobalToLocal( &thePt );
  1510.  
  1511.     // move the coordinate system according to the scroll bars
  1512.     GetOrigin( &saveOrigin );
  1513.     Doc_SetGrafOrigin( theDoc );
  1514.  
  1515.     // get the point again and adjust for our coord system.
  1516.     docPt = theEvent->where;
  1517.     GlobalToLocal( &docPt );
  1518.  
  1519.     // set the coordinate system back.
  1520.     SetOrigin( saveOrigin.h, saveOrigin.v );
  1521.  
  1522.     // $$$$$ if we don't update the window before scrolling it, we'll get
  1523.     // left over white space from the balloon.
  1524.     // reset the balloons since the selection could be moving.
  1525.     hideBalloons( theWindow );
  1526.     
  1527.     elemPart = ElemColl_FindElem(theDoc->contentColl, docPt, &elem);
  1528.     
  1529.     if (elem)
  1530.     {
  1531.     
  1532.         if (elemPart == inElement)
  1533.         {
  1534.             Elem_HandleMouseDown (elem, theDoc, theEvent);
  1535.         }
  1536.         else if (elemPart == inCornerResize)
  1537.         {
  1538.             Elem_HandleMouseInCornerResize (elem, theDoc, &docPt, theEvent);
  1539.             SetRectRgn( gMouseRgn, 0, 0, 1, 1 );
  1540.         }
  1541.         else if (elemPart == inEdgeResize)
  1542.         {
  1543.             Elem_HandleMouseInEdgeResize (elem, theDoc, &docPt, theEvent);
  1544.             SetRectRgn( gMouseRgn, 0, 0, 1, 1 );
  1545.         }
  1546.             
  1547.     }
  1548.  
  1549.     // we made it this for so we must be creating a new item.
  1550.  
  1551.     else if (gCurrentTool == 0)
  1552.     {
  1553.         // If we're not focused, get the focus and that's all
  1554.         if (gFocusDocument != theDoc->partDocRef)
  1555.             CARequestStandardFocusSet (theWindow);
  1556.         else
  1557.         {
  1558.             Doc_SelectAllElements( theDoc, false );
  1559.             theDoc->insertionPoint = docPt;
  1560.             Doc_HandleSelectionRect (theDoc, theEvent->where);
  1561.         }
  1562.     }
  1563.     else
  1564.     {
  1565.         if (gFocusDocument != theDoc->partDocRef)
  1566.             CARequestStandardFocusSet (theWindow);
  1567.         else
  1568.         {
  1569.             // If anything is selected then deselect everything
  1570.             if (Doc_SelectionExists(theDoc))
  1571.             {
  1572.                 // make sure none of the items are selected
  1573.                 Doc_SelectAllElements( theDoc, false );
  1574.             }
  1575.             // else add a PICT element according to the current tool
  1576.             else
  1577.             {
  1578.                 // save the ClipRgn
  1579.                 saveClip = NewRgn();
  1580.                 GetClip( saveClip );
  1581.             
  1582.                 // move the coordinate system according to the scroll bars.
  1583.                 SetOrigin(
  1584.                     GetControlValue( theDoc->hScrollBar ),
  1585.                     GetControlValue( theDoc->vScrollBar ) );
  1586.             
  1587.                 // clip to everything but the scroll bars and grow box
  1588.                 theRect = theDoc->contentRect;
  1589.                 OffsetRect(
  1590.                     &theRect,
  1591.                     GetControlValue( theDoc->hScrollBar ),
  1592.                     GetControlValue( theDoc->vScrollBar ) );
  1593.                 ClipRect( &theDoc->contentRect );
  1594.             
  1595.                 // add the item to our document.
  1596.                 Doc_AddItemPict( theDoc, gItemPICT[gCurrentTool], docPt );
  1597.         
  1598.                 // set the coordinate system back.
  1599.                 SetOrigin( saveOrigin.h, saveOrigin.v );
  1600.         
  1601.                 // put the clipRgn back and get rid of the saved rgn.
  1602.                 SetClip( saveClip );
  1603.                 DisposeRgn( saveClip );
  1604.                 Doc_SetDirty( theDoc, true );
  1605.             }
  1606.         }
  1607.     }
  1608.     
  1609.     SetPort (savePort);
  1610.  
  1611. }
  1612.  
  1613. //---------------------------------------------------------------------------
  1614. // called when there is a mouse down in the content region of an app window.
  1615. void App_MouseInContent(
  1616.     EventRecord        *theEvent,
  1617.     WindowPtr        theWindow )
  1618. {
  1619. Point        thePt;
  1620. DocPtr        theDoc;
  1621. Rect        docRect;
  1622.  
  1623.     theDoc = (DocPtr)GetWRefCon( theWindow );
  1624.  
  1625.     docRect = theDoc->contentRect;
  1626.     RectLocalToGlobal( &docRect );
  1627.     thePt = theEvent->where;
  1628.  
  1629.     // if it's in the main part of the document, handle it
  1630.     if (PtInRect( thePt, &docRect ))
  1631.         App_MouseInDocument( theEvent, theWindow );
  1632.     else    // otherwise it must be in the scroll bars
  1633.         App_MouseInScrollBars( theEvent, theWindow );
  1634. }
  1635.  
  1636. //---------------------------------------------------------------------------
  1637. // File Menu Items
  1638. //---------------------------------------------------------------------------
  1639. // App_NewMenu - create a new document.
  1640.  
  1641. void App_NewMenu( void )
  1642. {
  1643. OSErr    theErr;
  1644.  
  1645.     theErr = Doc_New();
  1646.     if (theErr) Error_ReportOperationError (theErr);
  1647.     
  1648. }
  1649.  
  1650. //---------------------------------------------------------------------------
  1651. // App_OpenMenu - allow the user to select a file. then open the file.
  1652.  
  1653. void App_OpenMenu( void )
  1654. {
  1655. SFTypeList            theTypeList;
  1656. StandardFileReply    theReply;
  1657. Point                thePt;
  1658. OSErr                theErr = noErr;
  1659. short                numTypes = 1;
  1660. FInfo                fInfo;
  1661.  
  1662.     theTypeList[0] = kOurFileType;
  1663.     
  1664. #ifdef USE_CALIB
  1665.     theTypeList[1] = kContainerFileType;
  1666.     numTypes = 2;
  1667. #endif
  1668.  
  1669.     if (!CARequestModalFocus( NULL))
  1670.         return;
  1671.             
  1672.     thePt.h = thePt.v = -1;        // center on the screen.
  1673.     CustomGetFile(
  1674.         nil, numTypes, theTypeList, &theReply, 0, thePt, nil,
  1675.         gWindowEventFilterYDUPP, nil, nil, nil );
  1676.  
  1677.     CARelinquishModalFocus( NULL );
  1678.  
  1679.     if (theReply.sfGood)
  1680.     {
  1681.         FSpGetFInfo (&theReply.sfFile, &fInfo);
  1682.         
  1683. #ifdef STATIONERY
  1684.         if (fInfo.fdFlags & kIsStationery)
  1685.         {
  1686.         
  1687.             // stationary pad - create new file
  1688.             page = theReply.sfFile;
  1689.             MakeDatedUniqueFSSpec (&page);
  1690.             CopyFile (&theReply.sfFile, &page);
  1691.             theErr = Doc_Open( &page );        // $$$$$ don't forget to handle errors.
  1692.  
  1693.         }
  1694. #endif
  1695.         theErr = Doc_Open( &theReply.sfFile );        // $$$$$ don't forget to handle errors.
  1696.     }
  1697.     
  1698.     if (theErr) Error_ReportOperationError (theErr);
  1699.     
  1700. }
  1701.  
  1702. //---------------------------------------------------------------------------
  1703. // App_CloseMenu - close theWindow, first asking the user if they want to save
  1704. // changes.
  1705.  
  1706. void App_CloseMenu(
  1707.     WindowPtr    theWindow )
  1708. {
  1709. WindowPtr    aWindow;
  1710. OSErr        theErr;
  1711. Boolean        cancelled;
  1712.  
  1713.     theErr = Win_Close( theWindow, &cancelled );
  1714.  
  1715.     if (theErr) Error_ReportOperationError (theErr);
  1716.     
  1717.     if (!cancelled)
  1718.     {
  1719.         // Make sure the next window gets selected
  1720.         aWindow = App_GetFrontDocWindow();
  1721.         if (aWindow != nil)
  1722.             App_SetFrontDocWindow( aWindow );
  1723.     }
  1724.     
  1725. }
  1726.  
  1727. //---------------------------------------------------------------------------
  1728. // App_SaveMenu - save theWindow's document.
  1729.  
  1730. void App_SaveMenu(
  1731.     WindowPtr    theWindow )
  1732. {
  1733. DocPtr        theDoc;
  1734. OSErr        theErr;
  1735.  
  1736.     // if there is no window or it's not ours, get out.
  1737.     if (Win_IsAppWindow( theWindow ))
  1738.     {
  1739.         theDoc = (DocPtr)GetWRefCon( theWindow );
  1740.         theErr = Doc_Save( theDoc );
  1741.         if (theErr) Error_ReportOperationError (theErr);
  1742.     }
  1743. }
  1744.  
  1745. //---------------------------------------------------------------------------
  1746. // App_SaveMenu - save theWindow's document under a new name.
  1747.  
  1748. void App_SaveAsMenu(
  1749.     WindowPtr    theWindow )
  1750. {
  1751. DocPtr        theDoc;
  1752. OSErr        theErr;
  1753.  
  1754.     // if there is no window or it's not ours, get out.
  1755.     if (Win_IsAppWindow( theWindow ))
  1756.     {
  1757.         theDoc = (DocPtr)GetWRefCon( theWindow );
  1758.         theErr = Doc_SaveAs( theDoc );
  1759.         if (theErr) Error_ReportOperationError (theErr);
  1760.     }
  1761. }
  1762.  
  1763. //---------------------------------------------------------------------------
  1764. // App_PageSetUpMenu - 
  1765.  
  1766. void App_PageSetUpMenu(    WindowPtr    theWindow )
  1767. {
  1768. DocPtr        theDoc;
  1769.  
  1770.     // if there is no window or it's not ours, get out.
  1771.     if (!Win_IsAppWindow( theWindow ))
  1772.         return;
  1773.  
  1774.     theDoc = (DocPtr)GetWRefCon( theWindow );
  1775.     if (!theDoc)
  1776.         return;
  1777.  
  1778.     App_ForceActivateFrontWindow( false );
  1779.  
  1780. #ifdef USE_CALIB
  1781.     if (gCALibExists)
  1782.     {
  1783.         if (!CARequestModalFocus( theWindow ))
  1784.             return;
  1785.     }
  1786. #endif
  1787.  
  1788.     PrOpen();
  1789.     if (PrError())
  1790.      {
  1791.          Error_ShowMessage( kCASMsgNoPrinterSelected );
  1792.         return;
  1793.     }
  1794.  
  1795.     if (checkPrintHandle( &theDoc->hPrint ))
  1796.     {
  1797.         if (PrStlDialog( theDoc->hPrint ))
  1798.             Doc_SetDirty( theDoc, true );
  1799.     }
  1800.     else
  1801.         SysBeep(2);
  1802.     PrClose();
  1803.  
  1804. #ifdef USE_CALIB
  1805.     if (gCALibExists)
  1806.         CARelinquishModalFocus( theWindow );
  1807. #endif
  1808.  
  1809.     App_ForceActivateFrontWindow( true );
  1810.  
  1811.     // re-adjust zoomed size of window according to printer.
  1812.     Win_AdjustStdState( theWindow );
  1813. }
  1814.  
  1815. //---------------------------------------------------------------------------
  1816.  
  1817. static void App_PreferencesMenu( void )
  1818. {
  1819. #define kSampleFlagItem        3
  1820.  
  1821. Boolean            prefSampleFlag;
  1822. DialogPtr        theDialog;
  1823. GrafPtr            savePort;
  1824. short            itemHit;
  1825. WindowPtr        frontWindow;
  1826.  
  1827.     GetPort( &savePort );
  1828.     App_ForceActivateFrontWindow( false );
  1829.  
  1830. #ifdef USE_CALIB
  1831.     if (gCALibExists)
  1832.     {
  1833.         frontWindow = CAGetFrontDocWindow();
  1834.         if (!CARequestModalFocus( frontWindow))
  1835.             return;
  1836.     }
  1837. #endif
  1838.  
  1839.     prefSampleFlag = gPrefSampleFlag;
  1840.  
  1841.     //bSetCursorWatch();
  1842.  
  1843.     Dialog_CenterDLOGonFrontWindow( kPreferencesDialog );
  1844.     theDialog = GetNewDialog( kPreferencesDialog, nil, nil );
  1845.  
  1846.     if (theDialog == nil)
  1847.         return;
  1848.  
  1849.     SetPort( theDialog );
  1850.  
  1851.     // DAS: changed from ShowWindow.
  1852.     // show it, now that everything is ready.
  1853.     SelectWindow( (WindowPtr)theDialog );
  1854.     Dialog_OutlineButton( theDialog, 1 );
  1855.  
  1856.     //SetCursor(&qd.arrow);
  1857.  
  1858.     iOKITEM = kOKButton;
  1859.     iCancelITEM = kCancelButton;
  1860.     do
  1861.     {
  1862.         // set style check boxes each time through
  1863.         SetControlValue(
  1864.             (ControlHandle)Dialog_GetItemHandle( theDialog, kSampleFlagItem ),
  1865.             prefSampleFlag ? 1 : 0 );
  1866.  
  1867.         ModalDialog( gWindowEventFilterUPP, &itemHit );
  1868.  
  1869.         switch (itemHit)
  1870.         {
  1871.             case kSampleFlagItem:
  1872.                 prefSampleFlag = !prefSampleFlag;    // toggle the flag.
  1873.                 break;
  1874.  
  1875.             default:
  1876.                 break;
  1877.         }
  1878.     }
  1879.     while (itemHit != kOKButton && itemHit != kCancelButton);
  1880.  
  1881. #ifdef USE_CALIB
  1882.     if (gCALibExists)
  1883.         CARelinquishModalFocus( frontWindow );
  1884. #endif
  1885.  
  1886.     DisposeDialog( theDialog );
  1887.     App_ForceActivateFrontWindow( true );
  1888.     SetPort( savePort );
  1889.  
  1890.     // if it's OK button…
  1891.     if (itemHit == kOKButton)
  1892.     {
  1893.         gPrefChanged = true;
  1894.         gPrefSampleFlag = prefSampleFlag;
  1895.     }
  1896. }
  1897.  
  1898. // margins in pixels
  1899. #define topMargin        0
  1900. #define leftMargin        0
  1901. #define rightMargin        0
  1902. #define bottomMargin    0
  1903.  
  1904. //---------------------------------------------------------------------------
  1905. // 
  1906. void App_PrintWindow(
  1907.     WindowPtr    theWindow )
  1908. {
  1909. Rect             printRect;
  1910. TPPrPort        printPort;
  1911. GrafPtr            savePort;
  1912. DocPtr            theDoc;
  1913. Rect            theRect;
  1914. Str255            windowTitle;
  1915. DialogPtr        printStatusDialog;
  1916. long            i;
  1917. short            count;
  1918. ElemPtr            elem;
  1919.  
  1920.     // if there is no window or it's not ours, get out.
  1921.     if (!Win_IsAppWindow( theWindow ))
  1922.         return;
  1923.  
  1924.     GetPort( &savePort );
  1925.  
  1926.     theDoc = (DocPtr)GetWRefCon( theWindow );
  1927.     if (!theDoc)
  1928.         return;
  1929.  
  1930.     App_ForceActivateFrontWindow( false );
  1931.  
  1932. #ifdef USE_CALIB
  1933.     if (gCALibExists)
  1934.     {
  1935.         if (!CARequestModalFocus( theWindow ))
  1936.         {
  1937.             return;
  1938.         }
  1939.     }
  1940. #endif
  1941.     
  1942.     // Put up a status dialog
  1943.     bSetCursorWatch();
  1944.     Dialog_CenterDLOGonFrontWindow( kPrintStatusDialog );
  1945.  
  1946.     printStatusDialog = GetNewDialog( kPrintStatusDialog, nil, nil );
  1947.  
  1948.     App_ForceActivateFrontWindow( false );
  1949.  
  1950.     SetPort( printStatusDialog );
  1951.     GetWTitle( theWindow, windowTitle );
  1952.     ParamText( windowTitle, nil, nil, nil );
  1953.     SelectWindow( (WindowPtr)printStatusDialog );
  1954.     DrawDialog( printStatusDialog );
  1955.  
  1956.     printPort = PrOpenDoc( theDoc->hPrint, nil, nil );
  1957.     SetPort( (GrafPtr)printPort );
  1958.     printRect = (**theDoc->hPrint).prInfo.rPage;
  1959.  
  1960.     // print page
  1961.     do
  1962.     {
  1963.         PrOpenPage( printPort, 0L );
  1964.  
  1965.         count = ElemColl_GetCount (theDoc->contentColl);
  1966.         
  1967.         // draw the content area here.
  1968.         for (i=1; i<=count; i++)
  1969.         {
  1970.             elem = ElemColl_GetNthElem (theDoc->contentColl, i);
  1971.             Elem_GetDisplayRect (elem, &theRect);
  1972.             if (RectInRgn( &theRect, theWindow->clipRgn ))
  1973.                 Elem_Draw(elem, true);
  1974.         }
  1975.  
  1976.         PrClosePage( printPort );
  1977.  
  1978.     }
  1979.     while (0);
  1980.  
  1981.     PrCloseDoc( printPort );
  1982.  
  1983.     // Bring down status dialog
  1984.     DisposeDialog( printStatusDialog );
  1985.  
  1986. #ifdef USE_CALIB
  1987.     if (gCALibExists)
  1988.         CARelinquishModalFocus( theWindow );
  1989. #endif
  1990.     App_ForceActivateFrontWindow( true );
  1991.  
  1992.     SetPort( savePort );
  1993.     SetCursor( &qd.arrow );
  1994. }
  1995.  
  1996.  
  1997. //---------------------------------------------------------------------------
  1998. // App_PrintMenu - 
  1999.  
  2000. void App_PrintMenu(
  2001.     WindowPtr    theWindow )
  2002. {
  2003. GrafPtr        savePort;
  2004. TPrStatus    prStatus;
  2005. short        copies;
  2006. DocPtr        theDoc;
  2007.  
  2008.     // if there is no window or it's not ours, get out.
  2009.     if (!Win_IsAppWindow( theWindow ))
  2010.         return;
  2011.  
  2012.     theDoc = (DocPtr)GetWRefCon( theWindow );
  2013.     if (!theDoc)
  2014.         return;
  2015.  
  2016.     App_ForceActivateFrontWindow( false );
  2017.  
  2018. #ifdef USE_CALIB
  2019.     if (gCALibExists)
  2020.     {
  2021.         if (!CARequestModalFocus( theWindow ))
  2022.         {
  2023.             return;
  2024.         }
  2025.     }
  2026. #endif
  2027.  
  2028.     PrOpen();
  2029.     if (PrError() != noErr)
  2030.      {
  2031.          Error_ShowMessage( kCASMsgNoPrinterSelected );
  2032.         return;
  2033.     }
  2034.     if (checkPrintHandle(&theDoc->hPrint))
  2035.     {
  2036.         SetCursor( &qd.arrow );
  2037.         if (PrJobDialog( theDoc->hPrint ) != 0)
  2038.         {
  2039. //            PleaseWait();
  2040.             GetPort( &savePort );
  2041.             for (copies = howManyCopies( theDoc->hPrint ); copies > 0; copies--)
  2042.             {
  2043.                 App_PrintWindow( theWindow );
  2044.                 PrPicFile( theDoc->hPrint, 0L, 0L, 0L, &prStatus );
  2045.             }
  2046.             SetPort( savePort );
  2047.         }
  2048.     }
  2049.     PrClose();
  2050.  
  2051. #ifdef USE_CALIB
  2052.     if (gCALibExists)
  2053.         CARelinquishModalFocus( theWindow );
  2054. #endif
  2055.  
  2056.     App_ForceActivateFrontWindow( true );
  2057. }
  2058.  
  2059. //---------------------------------------------------------------------------
  2060. // App_QuitMenu - close all the windows and set gExitFlag to true if the user
  2061. // doesn't cancel.
  2062.  
  2063. void App_QuitMenu( void )
  2064. {
  2065.     gExitFlag = App_CloseAll();
  2066. }
  2067.  
  2068. //---------------------------------------------------------------------------
  2069. // App_GetFrontFloater
  2070.  
  2071. WindowPtr App_GetFrontFloater( void )
  2072. {
  2073. WindowPtr    curFrontW;
  2074.  
  2075.     curFrontW = (WindowPtr)LMGetWindowList();
  2076.     if ((curFrontW != nil) && !Win_IsFloater( curFrontW ))
  2077.         curFrontW = nil;
  2078.  
  2079.     return curFrontW;
  2080. }
  2081.  
  2082. //---------------------------------------------------------------------------
  2083. // App_GetBottomFloater
  2084.  
  2085. WindowPtr App_GetBottomFloater( void )
  2086. {
  2087. WindowPtr    curFrontW, nextW;
  2088.  
  2089.     curFrontW = (WindowPtr)LMGetWindowList();
  2090.     while (curFrontW != nil)
  2091.     {
  2092.         nextW = (WindowPtr)(((WindowPeek)curFrontW)->nextWindow);
  2093.         if ((nextW == nil) || !Win_IsFloater( nextW ))
  2094.             break;
  2095.  
  2096.         curFrontW = nextW;
  2097.     }
  2098.  
  2099.     if ((curFrontW != nil) && !Win_IsFloater( curFrontW ))
  2100.         curFrontW = nil;
  2101.  
  2102.     return curFrontW;
  2103. }
  2104.  
  2105. //---------------------------------------------------------------------------
  2106. // App_GetFrontDocWindow
  2107.  
  2108. WindowPtr App_GetFrontDocWindow( void )
  2109. {
  2110. WindowPtr    curFrontW;
  2111.  
  2112. #ifdef USE_CALIB
  2113.     if (gCALibExists)
  2114.     {
  2115.         curFrontW = CAGetFrontDocWindow();
  2116.     }
  2117.     else
  2118. #endif
  2119.     {
  2120.         curFrontW = LMGetWindowList();
  2121.         while ( (curFrontW != nil) &&
  2122.                 Win_IsFloater( curFrontW ) &&
  2123.                 !Win_IsAppWindow( curFrontW) )
  2124.             curFrontW = (WindowPtr)(((WindowPeek)curFrontW)->nextWindow);
  2125.     }
  2126.     return curFrontW;
  2127. }
  2128.  
  2129. //---------------------------------------------------------------------------
  2130. // App_SetFrontDocWindow
  2131.  
  2132. void App_SetFrontDocWindow(
  2133.     WindowPtr    targetWindow )
  2134. {
  2135. WindowPtr    curFrontW, nextW;
  2136.  
  2137.     if ((targetWindow == nil) || !Win_IsVisible( targetWindow ) || Win_IsFloater( targetWindow ))
  2138.         return;
  2139.         
  2140. #ifdef USE_CALIB
  2141.  
  2142.     CASelectWindow (targetWindow);
  2143.     return;
  2144.     
  2145. #endif
  2146.  
  2147.     curFrontW = (WindowPtr)LMGetWindowList();
  2148.  
  2149.     if ((curFrontW == nil) || !Win_IsFloater( curFrontW ))
  2150.         SelectWindow( targetWindow );
  2151.     else
  2152.     {
  2153.         while ((curFrontW != nil) && Win_IsFloater( curFrontW ))
  2154.         {
  2155.             nextW = (WindowPtr)(((WindowPeek)curFrontW)->nextWindow);
  2156.             if ((nextW == nil) || !Win_IsFloater( nextW ))
  2157.             {
  2158.                 SendBehind( targetWindow, curFrontW );
  2159.                 CalcVis( (MODERN_WINDOW_REF)targetWindow );
  2160.                 HiliteWindow( targetWindow, true );
  2161.  
  2162. #if defined(_POST_ACTIVATE_EVENTS)
  2163.                 Win_SendActivateEvent( targetWindow, true );
  2164. #else
  2165.                 if (!Win_IsToolWindow( targetWindow ))
  2166.                     App_Activate( targetWindow );
  2167. #endif
  2168.  
  2169.  
  2170.                 if (nextW == targetWindow)
  2171.                     nextW = (WindowPtr)(((WindowPeek)nextW)->nextWindow);
  2172.  
  2173.                 if ((nextW != nil) && !Win_IsFloater( nextW ))
  2174.                 {
  2175.                     HiliteWindow( nextW, false );
  2176.  
  2177. #if defined(_POST_ACTIVATE_EVENTS)
  2178.                     Win_SendActivateEvent( nextW, false );
  2179. #else
  2180.                     if (!Win_IsToolWindow( targetWindow ))
  2181.                         App_Deactivate( nextW );
  2182. #endif
  2183.                 }
  2184.  
  2185.                 break;
  2186.             }
  2187.  
  2188.             curFrontW = (WindowPtr)(((WindowPeek)curFrontW)->nextWindow);
  2189.         }
  2190.     }
  2191. }
  2192.  
  2193. //---------------------------------------------------------------------------
  2194. // App_ShowHideToolWindows
  2195.  
  2196. void App_ShowHideToolWindows(
  2197.     Boolean        showThem )
  2198. {
  2199. WindowPtr    curFrontW;
  2200.  
  2201.     curFrontW = (WindowPtr)LMGetWindowList();
  2202.     while (curFrontW != nil)
  2203.     {
  2204.         if (Win_IsFloater( curFrontW ))
  2205.             Win_ShowHide( curFrontW, showThem );
  2206.  
  2207.         curFrontW = (WindowPtr)(((WindowPeek)curFrontW)->nextWindow);
  2208.     }
  2209. }
  2210.  
  2211. //---------------------------------------------------------------------------
  2212. // Appl_ForceActivateFrontWindow
  2213.  
  2214. void App_ForceActivateFrontWindow(
  2215.     Boolean        makeActive )
  2216. {
  2217. WindowPtr    curFrontDocW;
  2218. GrafPtr        savePort;
  2219.  
  2220.     curFrontDocW = App_GetFrontDocWindow();
  2221.     if (curFrontDocW != nil)
  2222.     {
  2223.         GetPort( &savePort );
  2224.         SetPort( (GrafPtr)curFrontDocW );
  2225.         HiliteWindow( curFrontDocW, makeActive );
  2226.         if (makeActive)
  2227.             App_Activate( curFrontDocW );
  2228.         else
  2229.             App_Deactivate( curFrontDocW );
  2230.         SetPort( savePort );
  2231.     }
  2232. }
  2233.  
  2234.